window.onload = function () {
    examTypeFoo()


}

//判断考试类型
function type(str){
    if(str=="mn"){
        return "模拟考试"
    }
    if(str=="zz"){
        return "正在考试"
    }
    if(str=="sx"){
        return "顺序练习"
    }
    if(str=="mr"){
        return "每日一测"
    }
    if(str=="fl"){
        return "分类练习"
    }
}




let examTypeFoo=async()=>{
   let result = await ajax({
       type:'GET',
       url:'http://127.0.0.1:4000/testPaper/GetTestPaperFind'
   })
   let resultJs=JSON.parse(result);
   console.log(resultJs)
   document.querySelector('.contents').addEventListener('click',function(event){
    if(event.target.getAttribute("class")=='exam-state'){
       let userId=event.target.getAttribute("sid");
       let stud=window.location.search;
       let stu =stud.slice(stud.indexOf("?")+1)
       console.log(window.location.search);
       window.location.href=`examInfo.html?${stu}&${userId}`;
    }
})
   examType(resultJs.obj);   

    
    
   let stuss=window.location.search;
   let stus =stuss.slice(stuss.indexOf("&")+1)
  
  let s_type = type(stus)
  let dom = document.querySelectorAll('.type')
 
  dom.forEach(v=>{
      v.innerHTML = s_type
  })


  let is_k = await ajax({
    type:'GET',
    url:'http://127.0.0.1:4000/tested/GetTested'
  })
  let url = window.location.search
  let stu_id = url.slice(url.indexOf("=")+1,url.indexOf('&'))
  
 let res_is = JSON.parse(is_k).obj.filter(v=>{
    
    return v.studentId._id == stu_id
  })
  res_is.forEach(v=>{
      document.querySelectorAll('.exam-state').forEach(e=>{
          if(v.testId._id==e.getAttribute('sid')){
              e.style.background = "#ccc"
              e.innerHTML = `已参加考试，得分为：${v.score}`
              e.classList = "test"
              e.style.textAlign ="center"
              e.style.color ="white"
          }
         
      })

  })
  
  document.querySelectorAll('.test').forEach(v=>{
    
     v.onclick = function () {
         let student_id =  url.slice(url.indexOf("=")+1,url.indexOf('&'))
         let url_id = v.getAttribute('sid')
         window.location.href = `JxtestPaper.html?${student_id}&${url_id}`
     }
  })


}
//渲染方法

function examType(p) {
    
    let str = "";
    p.forEach((v,i) => {

        let itemElem = `
        <ul class="content-item">
        <li class="title-type">考试类型：   
           <span class="type red"></span>
        </li>
        <li>
            <span>考试名称: </span>
            <span class="red">&nbsp;&nbsp;${v.title.length>21?v.title.substring(0,19)+'...':v.title}</span>
        </li>
        <li>
            <span>考试地址: </span>
            <span class="red">&nbsp;&nbsp;信息中心会议室</span>
        </li>
        <li>
            <span>已考人数: </span>
            <span class="red">&nbsp;&nbsp;343人</span>
        </li>
        <li class="exam-state" sid=${v._id}>参加考试</li>
        </ul>
        
        `
        str += itemElem;
    });
    document.querySelector('.contents').innerHTML = str;
}

